package com.dmjj.dmjjxt.controller;

import com.dmjj.common.base.BaseController;
import com.dmjj.common.result.ResultEnum;
import com.dmjj.common.result.ResultObject;
import com.dmjj.common.util.ListCopyToVOUtil;
import com.dmjj.common.util.PaginationVOUtil;
import com.dmjj.common.vo.ResultVO;
import com.dmjj.dmjjxt.dto.StoreItemDTO;
import com.dmjj.dmjjxt.service.StoreItemService;
import com.dmjj.dmjjxt.vo.StoreItemVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;

/**
 * @Description 店面分类字典表控制器
 * @Author zhangxue
 * @Date 2019-04-16 11:27:07
 */

@RestController
@Api(tags = "店面分类字典表控制器")
@RequestMapping("storeItem")
public class StoreItemController extends BaseController {

    private static final Log LOGGER = LogFactory.getLog(StoreItemController.class);

    @Autowired
    private StoreItemService service;
    @Autowired
    private ResultObject resultObject;

    /**
	 * 店面分类字典表列表
     * @Author zhangxue
     * @Date 2019-04-16 11:27:07
	 * @Param
	 * @Return
	 */
    @ApiOperation(value = "店面分类字典表列表", response = StoreItemVO.class)
    @PostMapping("list4Page")
    public ResultVO<Object> list4Page(
            StoreItemVO entity,
            @ApiParam(value = "当前页", required = true) @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页显示条数", required = true) @RequestParam(value = "pageSize", defaultValue = "15") Integer pageSize
    ){
        try {
            StoreItemDTO entityDTO = new StoreItemDTO();
            BeanUtils.copyProperties(entity, entityDTO);
            List<StoreItemDTO> list = service.list4Page(entityDTO, pageNum, pageSize);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), PaginationVOUtil.getPaginationVO(list, StoreItemVO.class, pageNum));
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 店面分类字典表列表（无分页）
     * @Author zhangxue
     * @Date 2019-04-16 11:27:07
     * @Param
     * @Return
     */
    @ApiOperation(value = "店面分类字典表列表（无分页）", response = StoreItemVO.class)
    @PostMapping("listByCondition")
    public ResultVO<Object> listByCondition(StoreItemVO entity){
        try {
            StoreItemDTO entityDTO = new StoreItemDTO();
            BeanUtils.copyProperties(entity, entityDTO);
            List<StoreItemDTO> list = service.listByCondition(entityDTO);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), ListCopyToVOUtil.getListToVO(list, StoreItemVO.class));
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 店面分类字典表详细信息
     * @Author zhangxue
     * @Date 2019-04-16 11:27:07
     * @Param
     * @Return
     */
    @ApiOperation(value = "店面分类字典表详细信息", response = StoreItemVO.class)
    @PostMapping("queryOneByPk")
    public ResultVO<Object> queryOneByPk(@ApiParam(value = "id",required = true) @RequestParam(value = "id") Long id){
        try {
            StoreItemDTO entityDTO = service.queryOneByPk(id);
            StoreItemVO entity = new StoreItemVO();
            BeanUtils.copyProperties(entityDTO, entity);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), entity);
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 保存店面分类字典表信息
     * @Author zhangxue
     * @Date 2019-04-16 11:27:07
     * @Param
     * @Return
     */
    @ApiOperation(value = "保存店面分类字典表信息", response = ResultVO.class)
    @PostMapping("save")
    public ResultVO<Object> save(StoreItemVO entity) {
        try {
            StoreItemDTO entityDTO = new StoreItemDTO();
            BeanUtils.copyProperties(entity, entityDTO);
            if(service.save(entityDTO) > 0) {
                return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg());
            }else {
                return resultObject.error(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg());
            }
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 删除店面分类字典表信息
     * @Author zhangxue
     * @Date 2019-04-16 11:27:07
     * @Param
     * @Return
     */
    @ApiOperation(value = "删除店面分类字典表信息", response = ResultVO.class)
    @PostMapping("delete")
    public ResultVO<Object> delete(@ApiParam(value = "id",required = true) @RequestParam(value = "id") Long id){
        try {
            if(service.delete(id) > 0) {
                return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg());
            }else {
                return resultObject.success(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg());
            }
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    @ApiOperation(value = "通过ids查询中文名称", response = StoreItemVO.class)
    @PostMapping("querChNameByIds")
    public ResultVO<Object> querChNameByIds(@ApiParam(value = "ids",required = true) @RequestParam(value = "ids") String ids){
        try {
            if(ids==null||"".equals(ids)){
                return resultObject.error(ResultEnum.EXCEPTION.getCode(), "ids不能为空");
            }
            String[]idArr = ids.split(",");
            String resultStr = "";
            for(int i= 0;i<idArr.length;i++){
                StoreItemDTO entitey = service.queryOneByPk(Long.valueOf(idArr[i]));
                resultStr += entitey.getName()+",";
            }
            resultStr = resultStr.substring(0,resultStr.length()-1);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), resultStr);
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

}